package com.mall.order.listener;

import com.mall.common.dto.SeckillOrderDTO;
import com.mall.order.feign.ProductFeignClient;
import com.mall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author: 谢君臣
 * @Date: 2022/9/20-14:02
 * @version: 1.0
 * @function:
 */
@Slf4j
@Component
@RabbitListener(queues = "order-seckill-order")
public class OrderSeckillListener {
    @Resource
    private OrderService orderService;


    @RabbitHandler
    public void listener(SeckillOrderDTO seckillOrderDTO, Channel channel, Message message) throws IOException {
        try {
            log.info("秒杀订单正在处理...");
            orderService.saveSeckillOrder(seckillOrderDTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
